/**
    Die Ewigen Extension
    Copyright (C) 2011 DonKrieg @ sonkyu@googlemail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var WARHEART_FIELD_NAME     = "nick";       // input
var WARHEART_FIELD_REPORT   = "kb";         // textarea
var WARHEART_SEND           = "Abschicken"  // input

chrome.extension.sendRequest({action: "getFightReport", page: "warheart"}, function(response) {
    // no report found
    if(response == undefined || response.report == undefined || response.report == "undefined") {
        return;
    }

    // set the name
    var tn = document.getElementsByName(WARHEART_FIELD_NAME);
    var fieldServer = tn[0];
    fieldServer.value = response.showname;

    // get the textarea and autofill the report
    var ta = document.getElementsByName(WARHEART_FIELD_REPORT);
    var fieldReport = ta[0];
    fieldReport.innerHTML = response.report;

    // should we autosubmit the form?
    if(response.autosubmit == "on") {
        // find the send button
        var inputList = document.getElementsByTagName("input")
        for(var i=0; i < inputList.length; i++) {
            if(inputList[i].getAttribute("value") == WARHEART_SEND) {
                inputList[i].click();
            }
        }
    }
    
});